package com.kingviker.example.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;

public class ByteBufTest1 {
    public static void main(String[] args) {
        //默认大小是传入字符串的长度乘以字符编码的最大长度 这里是11*3
//        ByteBuf buffer = Unpooled.copiedBuffer("hello,world", Charset.forName("utf-8"));
         //默认大小是传入字符串的长度乘以字符编码的最大长度 这里是12*3, 虽然张这里是汉子占用三个字节,可是只是一个字符 所以是12
        ByteBuf buffer = Unpooled.copiedBuffer("张hello,world", Charset.forName("utf-8"));

        //堆上缓冲区都是用字节数据存放的,所以如果是true代表是headBuffer
        if (buffer.hasArray()) {
            byte[] content = buffer.array();
            System.out.println(new String(content,Charset.forName("utf-8")));


            System.out.println(buffer);

            System.out.println(buffer.arrayOffset());
            System.out.println(buffer.readerIndex());
            System.out.println(buffer.writerIndex());
            System.out.println(buffer.capacity());

            int length = buffer.readableBytes();
            System.out.println("readableBytes:"+length);

            for (int i = 0; i < buffer.readableBytes(); i++) {
                System.out.println((char)buffer.getByte(i));
            }


            System.out.println(buffer.getCharSequence(0,4,Charset.forName("utf-8")));
            System.out.println(buffer.getCharSequence(4,6,Charset.forName("utf-8")));
        }


    }
}
